//
//  TravelNotesParser.m
//  Trip
//
//  Created by dllo on 15/7/17.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "TravelNotesParser.h"
#import "TravelNotesAll.h"
@implementation TravelNotesParser
+ (NSMutableArray *)getParser:(NSData *)data
{
    NSMutableArray *array = [NSMutableArray array];
    NSMutableArray *array2 = [NSMutableArray array];
    NSMutableArray *array3 = [NSMutableArray array];
    NSMutableArray *array4 = [NSMutableArray array];
    NSMutableArray *array5 = [NSMutableArray array];
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    TravelNotesAll *model = [TravelNotesAll getModel];
    model.front_cover_photo_url = [dic objectForKey:@"front_cover_photo_url"];
    model.name = [dic objectForKey:@"name"];
    if ([dic objectForKey:@"start_date"] != nil && ![[dic objectForKey:@"start_date"] isEqual:[NSNull null]]) {
        model.start_date = [dic objectForKey:@"start_date"];
        model.end_date = [dic objectForKey:@"end_date"];
    }
    model.image = [dic objectForKey:@"image"];
    model.photos_count = [[dic objectForKey:@"photos_count"] integerValue];
    model.userImage = [[dic objectForKey:@"user"] objectForKey:@"image"];
    model.userName = [[dic objectForKey:@"user"] objectForKey:@"name"];
    NSArray *arr = [dic objectForKey:@"trip_days"] ;
    for (NSDictionary *dic2 in arr) {
        array2 = [NSMutableArray array];
        TravelNotesAll *model1 = [TravelNotesAll getModel];
        model1.day = [[dic2 objectForKey:@"day"] integerValue];
        model1.trip_date = [dic2 objectForKey:@"trip_date"];
        model1.name_zh_cn = [[dic2 objectForKey:@"destination"] objectForKey:@"name_zh_cn"];
        NSArray *arrNotes = [dic2 objectForKey:@"nodes"];
        NSInteger num1 = 0;
        int i = 1;
        array4 = [NSMutableArray array];
        for (NSDictionary *dicNotes in arrNotes) {
            TravelNotesAll *model4 = [TravelNotesAll getModel];
            model1.entry_type = [dicNotes objectForKey:@"entry_type"];
            model1.entry_name = [dicNotes objectForKey:@"entry_name"];
            model4.entry_name = [dicNotes objectForKey:@"entry_name"];
            model4.day = model1.day;
            if (![[dicNotes objectForKey:@"entry_name"] isEqual:[NSNull null]]) {
                num1++;
                [array4 addObject:model4];
            }
            if (arrNotes.count == i++) {
                if (num1 == 0) {
                    [array4 addObject:model4];
                }
                model1.num = num1;
            }
            NSInteger num = 0;
            model1.comment = [dicNotes objectForKey:@"comment"];
            model1.lat = [[dicNotes objectForKey:@"lat"] integerValue];
            model1.lng = [[dicNotes objectForKey:@"lng"] integerValue];
            NSArray *arrNotess = [dicNotes objectForKey:@"notes"];
            if (arrNotess.count != 0) {
                for (NSDictionary *dicNotess in arrNotess) {
                    array = [NSMutableArray array];
                    TravelNotesAll *model2 = [TravelNotesAll getModel];
                    model2.col = [dicNotess objectForKey:@"col"];
                    model2.descrip = [dicNotess objectForKey:@"description"];
                    model2.image_file_size = [[[dicNotess objectForKey:@"photo"] objectForKey:@"image_file_size"] integerValue];
                    model2.image_height = [[[dicNotess objectForKey:@"photo"] objectForKey:@"image_height"] integerValue];
                    model2.image_width = [[[dicNotess objectForKey:@"photo"] objectForKey:@"image_width"] integerValue];
                    model2.url = [[dicNotess objectForKey:@"photo"] objectForKey:@"url"];
                    model2.day = model1.day;
                    model2.trip_date = model1.trip_date;
                    model2.user_entry = [[dicNotess objectForKey:@"user_entry"] boolValue];
                    if (num == 0) {
                        model2.comment = model1.comment;
                        num = 1;
                        model2.entry_type = model1.entry_type;
                        model2.entry_name = model1.entry_name;
                    }
                    [array addObject:model];
                    [array addObject:model1];
                    [array addObject:model2];
                    [array2 addObject:array];
                }
            } else {
                TravelNotesAll *modelk = [TravelNotesAll getModel];
                array = [NSMutableArray array];
                modelk.entry_name = model1.entry_name;
                modelk.entry_type = model1.entry_type;
                modelk.day = model1.day;
                [array addObject:model];
                [array addObject:model1];
                [array addObject:modelk];
                [array2 addObject:array];
            }
        }
        [array5 addObject:array4];
        [array3 addObject:array2];
    }
    [array3 addObject:array5];
    return array3;
}
@end
